package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.TestPaperDto;
import com.tcm.entity.dto.param.TestPaperPageParam;
import com.tcm.entity.dto.param.TestPaperShareParam;
import com.tcm.entity.resp.R;
import com.tcm.service.TestPaperService;
import com.tcm.service.TestPaperShareService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/8/2 11:36
 */
@RestController
@RequestMapping("/pc/testPaper")
public class TestPaperController {

    @Autowired
    private TestPaperService testPaperService;

    @Autowired
    private TestPaperShareService testPaperShareService;

    /**
     * 试卷模块获取教师列表
     * @param request http请求对象
     * @return 教师列表
     */
    @RequestMapping(value = "/teacher", method = RequestMethod.GET)
    public R getTeacherListByTestPaper(HttpServletRequest request){
        return R.success(testPaperService.getTeacherListByTestPaper(AccessTokenUtil.getUserId(request)));
    }

    /**
     * 添加试卷
     * @param dto 试卷数据封装类
     * @param request http请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addTestPaper(@RequestBody @Valid TestPaperDto dto, HttpServletRequest request){
        return testPaperService.addTestPaper(dto, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 修改试卷信息
     * @param dto 试卷数据封装类
     * @param request http请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyTestPaper(@RequestBody @Valid TestPaperDto dto, HttpServletRequest request){
        if (dto.getId() == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return testPaperService.modifyTestPaper(dto, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 删除试卷
     * @param id 试卷ID
     * @param request 请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeTestPaper(@RequestParam Long id, HttpServletRequest request){
        return testPaperService.removeTestPaper(id, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 根据角色分页获取试卷列表
     * @param param 分页查询参数
     * @param request http请求对象
     * @return 试卷列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getTestPaperByRole(TestPaperPageParam param, HttpServletRequest request){
        if (param.getPage() == null || param.getPageSize() == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return R.success(testPaperService.getTestPaperByRole(param, AccessTokenUtil.getUserId(request)));
    }

    /**
     * 根据试卷ID获取试卷信息
     * @param id 试卷ID
     * @return 试卷信息
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public R getTestPaperById(@RequestParam Long id){
        TestPaperDto testPaper = testPaperService.getTestPaperById(id);
        if (testPaper == null) {
            return R.fail(ResponseEnum.PARAM_IS_INVAlID);
        }
        return R.success(testPaper);
    }

        /**
     * 向教师分享试卷
     * @param param 参数
     * @param request 请求对象
     * @return 处理结果
     */
    @RequestMapping(value = "/share", method = RequestMethod.POST)
    public R shareTestPaper(@RequestBody @Valid TestPaperShareParam param, HttpServletRequest request){
        if (CollectionUtils.isEmpty(param.getTeacherIds())) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return testPaperShareService.shareTestPaper(param, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 根据当前用户获取已分享列表
     * @param request 请求对象
     * @return 已分享列表
     */
    @RequestMapping(value = "/share", method = RequestMethod.GET)
    public R getSharedList(HttpServletRequest request){
        return R.success(testPaperShareService.getSharedList(AccessTokenUtil.getUserId(request)));
    }

    /**
     * 取消分享
     * @param ids 分享ID
     * @return 处理结果
     */
    @RequestMapping(value = "/share", method = RequestMethod.DELETE)
    public R canselShareTestPaper(@RequestParam("ids")List<Long> ids){
        return testPaperShareService.canselShareTestPaper(ids) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 获取接收到的分享列表
     * @param request 请求对象
     * @return 取接收到的分享列表
     */
    @RequestMapping(value = "/received", method = RequestMethod.GET)
    public R getReceivedShareList(HttpServletRequest request){
        return R.success(testPaperShareService.getReceivedShareList(AccessTokenUtil.getUserId(request)));
    }

    /**
     * 确认接收试卷分享
     * @param shareId 试卷分享ID
     * @param request 请求对象
     * @return 处理结果，true为成功
     */
    @RequestMapping(value = "/received", method = RequestMethod.POST)
    public R confirmReceiveShare(@RequestParam Long shareId, HttpServletRequest request){
        return testPaperShareService.confirmReceiveShare(shareId, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.HANDLE_FAIL);
    }
}
